package com.pentakill.mo.util.base;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pentakill.mo.essay.essay.vo.EssayModel;

import java.lang.reflect.ParameterizedType;  
public abstract class BaseImpl<T> extends HibernateDaoSupport implements BaseDao<T>{
	
	private Class <T> entityClass = null;
	public BaseImpl(){
		Type genType = getClass().getGenericSuperclass();   
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();   
		entityClass =  (Class)params[0];  
	}	
	
	public void add(T t) {
		this.getHibernateTemplate().save(t);
	}

	
	public void delete(T t) {
		this.getHibernateTemplate().delete(t);
		
	}

	
	public void update(T t) {
		this.getHibernateTemplate().update(t);
		
	}

	
	public T get(Serializable uuid) {
		return this.getHibernateTemplate().get(entityClass,uuid);
	}

	
	public List<T> getAll() {
		DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	
	public List<T> getAll(BaseQueryModel qm, Integer pageNum, Integer prePageNum) {
		DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);
		doQbc(criteria,qm);
		return this.getHibernateTemplate().findByCriteria(criteria,(pageNum-1)*prePageNum,prePageNum);
	}

	
	public Integer getCount(BaseQueryModel qm) {
		DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);
		criteria.setProjection(Projections.rowCount());
		doQbc(criteria,qm);
		List<Long> longs = this.getHibernateTemplate().findByCriteria(criteria);
		return longs.get(0).intValue();
	}
	
	/**
	 * 
		if (dqm.getName() != null || dqm.getTele() != null) {
			String dqmName = dqm.getName().trim();
			String dqmTele = dqm.getTele().trim();
			if (dqmName != null && dqmName.length() > 0) {
				dc.add(Restrictions.like("name", "%" + dqmName + "%"));
			}
			if (dqmTele != null && dqmTele.length() > 0) {
				dc.add(Restrictions.like("name", "%" + dqmTele + "%"));
			}
		}
	 * @param criteria 添加参数
	 * @param qm 查询数据
	 */
	protected abstract void doQbc(DetachedCriteria criteria, BaseQueryModel qm);
	


}
